package com.example.front.main.home;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.example.front.R;
import com.example.front.WelcomeActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;

public class HomeFragment extends Fragment implements OnBannerListener{

    private Banner banner;
    private ArrayList<String> list_path;
    private ArrayList<String> list_title;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        return root;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final Button boardgame_rank_button = (Button) getActivity().findViewById(R.id.boardgame_rank_button);
        boardgame_rank_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BoardgameActivity.class);
                startActivity(intent);
            }
        });
        final Button scenario_rank_button = (Button) getActivity().findViewById(R.id.scenario_rank_button);
        scenario_rank_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ScenarioActivity.class);
                startActivity(intent);
            }
        });
        final Button psgame_rank_button = (Button) getActivity().findViewById(R.id.psgame_rank_button);
        psgame_rank_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), PsGameActivity.class);
                startActivity(intent);
            }
        });
        final Button switchgame_rank_button = (Button) getActivity().findViewById(R.id.switchgame_rank_button);
        switchgame_rank_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SwitchGameActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onViewCreated(@NonNull View view, @NonNull Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        banner = (Banner) getActivity().findViewById(R.id.banner);
        //放图片地址的集合
        list_path = new ArrayList<>();
        //放标题的集合
        list_title = new ArrayList<>();

        list_path.add("https://cloud.662359.cn/view.php/6b06049a01c9e82145999937118e2c61.jpg");
        list_path.add("https://cloud.662359.cn/view.php/172048f60c66e66b3a6114e69c903f2f.jpg");
        list_path.add("https://cloud.662359.cn/view.php/085219630dad97966d294549d6695cb0.jpg");
        list_title.add("最后的生还者");
        list_title.add("幽灵线：东京");
        list_title.add("小缇娜的奇幻之地");
        //设置内置样式，共有六种可以点入方法内逐一体验使用。
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        //设置图片加载器，图片加载器在下方
        banner.setImageLoader(new MyLoader());
        //设置图片网址或地址的集合
        banner.setImages(list_path);
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Stack);
        //设置轮播图的标题集合
        banner.setBannerTitles(list_title);
        //设置轮播间隔时间
        banner.setDelayTime(2000);
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);
        //设置指示器的位置，小点点，左中右。
        banner.setIndicatorGravity(BannerConfig.CENTER)
                //以上内容都可写成链式布局，这是轮播图的监听。比较重要。方法在下面。
                .setOnBannerListener(this)
                //必须最后调用的方法，启动轮播图。
                .start();
    }

    //轮播图的监听方法
    @Override
    public void OnBannerClick(int position) {
        switch (position){
            case 0:
                Uri uri1 = Uri.parse("https://www.3dmgame.com/games/thelastofus/");
                Intent intent1 = new Intent(Intent.ACTION_VIEW, uri1);
                startActivity(intent1);
                break;
            case 1:
                Uri uri2 = Uri.parse("https://www.3dmgame.com/games/ghostwiretokyo/");
                Intent intent2 = new Intent(Intent.ACTION_VIEW, uri2);
                startActivity(intent2);
                break;
            case 2:
                Uri uri3 = Uri.parse("https://www.3dmgame.com/games/ttsw/");
                Intent intent3 = new Intent(Intent.ACTION_VIEW, uri3);
                startActivity(intent3);
                break;
        }
    }

    //自定义的图片加载器
    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load((String) path).into(imageView);
        }
    }
}